import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { SysUserOnline, OnlineUserQuery, OnlineUserStatistics } from './types';

// 定义Other模块的数据类型
interface OtherDeptVO {
  deptId: number;
  deptName: string;
  parentId: number;
}

interface OtherUserVO {
  userId: number;
  userName: string;
  nickName: string;
  deptId: number;
  deptName?: string;
  email?: string;
  phonenumber?: string;
  avatar?: string;
}

// 获取部门树列表
export function deptTreeSelect() {
  return request({
    url: '/other/user/deptTree',
    method: 'get'
  });
}

// 根据部门ID获取用户列表
export function listUserByDeptId(deptId: string | number) {
  return request({
    url: `/other/user/list/${deptId}`,
    method: 'get'
  });
}

// 类型定义
export interface DeptTreeSelectResponse {
  code: number;
  data: OtherDeptVO[];
  msg: string;
}

export interface UserListResponse {
  code: number;
  data: OtherUserVO[];
  msg: string;
}

/**
 * 获取在线用户列表
 * @param query 查询参数
 */
export const getOnlineUserList = (query?: OnlineUserQuery): AxiosPromise<SysUserOnline[]> => {
  return request({
    url: '/monitor/online/list',
    method: 'get',
    params: query
  });
};

/**
 * 获取当前用户在线设备
 */
export const getCurrentUserOnlineDevices = (): AxiosPromise<SysUserOnline[]> => {
  return request({
    url: '/monitor/online',
    method: 'get'
  });
};

/**
 * 强退指定用户
 * @param tokenId token值
 */
export const forceLogoutUser = (tokenId: string) => {
  return request({
    url: `/monitor/online/${tokenId}`,
    method: 'delete'
  });
};

/**
 * 强退当前用户的指定设备
 * @param tokenId token值
 */
export const removeCurrentUserDevice = (tokenId: string) => {
  return request({
    url: `/monitor/online/myself/${tokenId}`,
    method: 'delete'
  });
};

/**
 * 获取在线用户数量
 */
export const getOnlineUserCount = (): AxiosPromise<number> => {
  return request({
    url: '/monitor/online/count',
    method: 'get'
  });
};

/**
 * 获取在线用户统计信息
 */
export const getOnlineUserStatistics = (): AxiosPromise<OnlineUserStatistics> => {
  return request({
    url: '/monitor/online/statistics',
    method: 'get'
  });
};

// 导出类型供其他组件使用
export type { OtherDeptVO, OtherUserVO };
